package com.smartwasser.yunhtgl.apphtgl.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.View;

import com.smartwasser.yunhtgl.apphtgl.R;

public class GestureLockView extends View {
    private static final String TAG = "GestureLockView";

    /**
     * GestureLockView的三种状态
     */
    enum Mode {
        STATUS_NO_FINGER, STATUS_FINGER_ON, STATUS_FINGER_UP, STATUS_PASS;
    }

    /**
     * GestureLockView的当前状态
     */
    private Mode mCurrentStatus = Mode.STATUS_NO_FINGER;

    /**
     * 宽度
     */
    private int mWidth;
    /**
     * 高度
     */
    private int mHeight;
    /**
     * 外圆半径
     */
    private int mRadius;
    /**
     * 画笔的宽度
     */
    private int mStrokeWidth = 2;

    /**
     * 圆心坐标
     */
    private int mCenterX;
    private int mCenterY;
    private Paint mPaint;

    /**
     * 箭头（小三角最长边的一半长度 = mArrawRate * mWidth / 2 ）
     */
    private float mArrowRate = 0.333f;
    private int mArrowDegree = -1;
    private Path mArrowPath;
    /**
     * 内圆的半径 = mInnerCircleRadiusRate * mRadus
     *
     */
    private float mInnerCircleRadiusRate = 0.3F;

//    /**
//     * 四个颜色，可由用户自定义，初始化时由GestureLockViewGroup传入
//     */
//    private int mColorNoFingerInner;
//    private int mColorNoFingerOutter;
//    private int mColorFingerOn;
//    private int mColorFingerUp;
    private Bitmap mNormalBtp;
    private Bitmap mErrorBtp;
    private Bitmap mCorrectBtp;
    private Bitmap mBgBtp;

    public GestureLockView(Context context, int colorNoFingerInner,
            int colorNoFingerOutter, int colorFingerOn, int colorFingerUp) {
        super(context);
//        this.mColorNoFingerInner = colorNoFingerInner;
//        this.mColorNoFingerOutter = colorNoFingerOutter;
//        this.mColorFingerOn = colorFingerOn;
//        this.mColorFingerUp = colorFingerUp;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArrowPath = new Path();

        mNormalBtp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_gesture_normal);
        mErrorBtp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_gesture_error);
        mCorrectBtp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_gesture_correct);
        mBgBtp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.bg_gesture);
    }

    public void setSize(int width, int height) {
        mWidth = width;
        mHeight = height;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        mWidth = MeasureSpec.getSize(widthMeasureSpec);
//        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);

        // 取长和宽中的小值
//        mWidth = mWidth < mHeight ? mWidth : mHeight;
        mRadius = mCenterX = mCenterY = mWidth / 2;
        mRadius -= mStrokeWidth / 2;

        // 绘制三角形，初始时是个默认箭头朝上的一个等腰三角形，用户绘制结束后，根据由两个GestureLockView决定需要旋转多少度
        float mArrowLength = mWidth / 2 * mArrowRate;
        mArrowPath.moveTo(mWidth / 2, mStrokeWidth + 2);
        mArrowPath.lineTo(mWidth / 2 - mArrowLength, mStrokeWidth + 2
                + mArrowLength);
        mArrowPath.lineTo(mWidth / 2 + mArrowLength, mStrokeWidth + 2
                + mArrowLength);
        mArrowPath.close();
        mArrowPath.setFillType(Path.FillType.WINDING);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        clearCircle(canvas);
        switch (mCurrentStatus) {
        case STATUS_FINGER_ON:
        case STATUS_PASS:
//            // 绘制外圆
//            mPaint.setStyle(Style.STROKE);
//            mPaint.setColor(mColorFingerOn);
//            mPaint.setStrokeWidth(2);
//            canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
//            // 绘制内圆
//            mPaint.setStyle(Style.FILL);
//            canvas.drawCircle(mCenterX, mCenterY, mRadius
//                    * mInnerCircleRadiusRate, mPaint);
            canvas.drawBitmap(mCorrectBtp,
                    new Rect(0, 0, mCorrectBtp.getWidth(), mCorrectBtp.getHeight()),
                    new Rect(0, 0, mWidth, mWidth), null);
            break;
        case STATUS_FINGER_UP:
//            // 绘制外圆
//            mPaint.setColor(mColorFingerUp);
//            mPaint.setStyle(Style.STROKE);
//            mPaint.setStrokeWidth(2);
//            canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
//            // 绘制内圆
//            mPaint.setStyle(Style.FILL);
//            canvas.drawCircle(mCenterX, mCenterY, mRadius
//                    * mInnerCircleRadiusRate, mPaint);
            canvas.drawBitmap(mErrorBtp,
                    new Rect(0, 0, mErrorBtp.getWidth(), mErrorBtp.getHeight()),
                    new Rect(0, 0, mWidth, mWidth), null);

//            drawArrow(canvas);

            break;

        case STATUS_NO_FINGER:

            canvas.drawBitmap(mNormalBtp,
                    new Rect(0, 0, mNormalBtp.getWidth(), mNormalBtp.getHeight()),
                    new Rect(0, 0, mWidth, mWidth), null);
//            // 绘制外圆
//            mPaint.setStyle(Style.FILL);
//            mPaint.setColor(mColorNoFingerOutter);
//            canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
//            // 绘制内圆
//            mPaint.setColor(mColorNoFingerInner);
//            canvas.drawCircle(mCenterX, mCenterY, mRadius
//                    * mInnerCircleRadiusRate, mPaint);
            break;

        }

    }

    private void clearCircle(Canvas canvas) {
        canvas.save();
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        Path path = new Path();
        path.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);
        canvas.clipPath(path); //裁剪区域
        canvas.drawBitmap(mBgBtp, 0, 0, paint);//把图画上去
        canvas.restore();
    }

    /**
     * 绘制箭头
     *
     * @param canvas
     */
    private void drawArrow(Canvas canvas) {
        if (mArrowDegree != -1) {
            mPaint.setStyle(Style.FILL);

            canvas.save();
            canvas.rotate(mArrowDegree, mCenterX, mCenterY);
            canvas.drawPath(mArrowPath, mPaint);

            canvas.restore();
        }

    }

    /**
     * 设置当前模式并重绘界面
     *
     * @param mode
     */
    public void setMode(Mode mode) {
        this.mCurrentStatus = mode;
        invalidate();
    }

    public void setArrowDegree(int degree) {
        this.mArrowDegree = degree;
    }

    public int getArrowDegree() {
        return this.mArrowDegree;
    }
}
